package com.ziyueyuwen.xiniao.controller.product.student;

import com.suyeer.basic.util.HttpResUtil;
import com.ziyueyuwen.xiniao.base.HttpResContent;
import com.ziyueyuwen.xiniao.bean.page.PageData;
import com.ziyueyuwen.xiniao.bean.product.*;
import com.ziyueyuwen.xiniao.bean.select.SelectBean;
import com.ziyueyuwen.xiniao.enums.GradeEnum;
import com.ziyueyuwen.xiniao.enums.ParamTypeEnum;
import com.ziyueyuwen.xiniao.enums.ProductClassRuleEnum;
import com.ziyueyuwen.xiniao.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.List;
import java.util.Map;

import static com.ziyueyuwen.xiniao.base.BaseUtil.checkParam;
import static com.ziyueyuwen.xiniao.base.BaseUtil.saveErrorMsgAndData;
import static com.ziyueyuwen.xiniao.base.BaseUtil.saveSuccessMsgAndData;
import static com.ziyueyuwen.xiniao.util.ConstUtil.*;

/**
 * @author wancheng  on 2019/2/23.
 */
@Controller
@RequestMapping("/product/student")
@Validated
public class ProductStudentController {

    @Autowired
    ProductService productService;

    @RequestMapping(value = "/get_param_grade.do", method = RequestMethod.POST)
    public void getParamGrade(HttpServletRequest request,
                              HttpServletResponse response) {
        HttpResContent<List<SelectBean>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("学生端商品列表获取年级参数");
        try {
            List<SelectBean> grades = productService.getProductQueryGrade();
            saveSuccessMsgAndData(hrc, grades);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_param_date.do", method = RequestMethod.POST)
    public void getParamDate(HttpServletRequest request,
                             HttpServletResponse response,
                             @RequestParam(PARAM_COURSE_ID) Integer courseId) {
        HttpResContent<List<SelectBean>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("学生端商品列表获取时间参数");
        try {
            checkParam(ParamTypeEnum.INTEGER_GT_0, PARAM_COURSE_ID, courseId);
            List<SelectBean> data = productService.getProductQueryDate(courseId);
            saveSuccessMsgAndData(hrc, data);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_param_time.do", method = RequestMethod.POST)
    public void getParamTime(HttpServletRequest request,
                             HttpServletResponse response,
                             @RequestParam(PARAM_COURSE_ID) Integer courseId,
                             @RequestParam(value = PARAM_PRODUCT_STAGE_ID, required = false) Integer stageId,
                             @RequestParam(value = PARAM_PRODUCT_CLASS_RULE, required = false) ProductClassRuleEnum classRule) {
        HttpResContent<List<SelectBean>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("学生端商品列表获取时间参数2");
        try {
            checkParam(ParamTypeEnum.INTEGER_GT_0, PARAM_COURSE_ID, courseId);
            List<SelectBean> data = productService.getProductQueryTime(courseId, stageId, classRule);
            saveSuccessMsgAndData(hrc, data);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_product_group_list.do", method = RequestMethod.POST)
    public void getProductGroupList(HttpServletRequest request,
                                    HttpServletResponse response,
                                    GradeEnum grade
    ) {
        HttpResContent<List<ProductStudentGroupVO>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("学生端商品组 查询列表");
        try {
            List<ProductStudentGroupVO> groups = productService.getProductGroup(grade);
            saveSuccessMsgAndData(hrc, groups);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_product_list.do", method = RequestMethod.POST)
    public void getProductList(HttpServletRequest request,
                               HttpServletResponse response,
                               @Validated ProductStudentListQuery productListQuery) {
        HttpResContent<PageData<ProductStudentListVO>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("学生端商品查询列表");
        try {
            PageData<ProductStudentListVO> data = productService.getStudentProductList(productListQuery);
            saveSuccessMsgAndData(hrc, data);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_product_info.do", method = RequestMethod.POST)
    public void getProductInfo(HttpServletRequest request,
                               HttpServletResponse response,
                               @RequestParam(PARAM_PRODUCT_ID) Integer productId) {
        HttpResContent<ProductStudentListVO> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取产品详情");
        try {
            checkParam(ParamTypeEnum.INTEGER_GT_0, PARAM_PRODUCT_ID, productId);
            String userAccount = hrc.getUserInfo().getUserAccount();
            ProductStudentListVO data = productService.getStudentProductInfoByProductId(productId,userAccount);
            saveSuccessMsgAndData(hrc, data);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }
}
